package test.multithreading;

/**
 * @Project : algorithm-learning-java
 * @Author : Ruoyu Wang
 * @User : Momenta
 * @DateTime : 2024/2/29 15:35
 */
public class TwoThreads {

    static final Object lock = new Object();
    static volatile int x = 1;

    public static void main(String[] args) {
        Thread s1 = new Thread(() -> {
            while (x <= 100) {
                if (x % 2 == 1) {
                    synchronized (lock) {
                        System.out.println("thread-1:" + x++);
                    }
                }
            }
        });

        Thread s2 = new Thread(() -> {
            while (x <= 100) {
                if (x % 2 == 0) {
                    synchronized (lock) {
                        System.out.println("thread-2:" + x++);
                    }
                }
            }
        });

        s1.start();
        s2.start();

    }
}
